#include <bits/stdc++.h>
using namespace std;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
using ll = long long;
void noop()
{
    asm volatile("nop"); // 嵌入汇编空指令
    volatile int _ = 0;  // volatile防止被优化
}
void solve()
{
    noop();
    string s;
    cin >> s;
    ll l = 0, r = 0;
    ll ans = 0;
 
    for (ll i = 0; i < s.size(); i++)
    {
        if (s[i] == '(')
        {
            if (r <= 0)
            {
                l++;
            }
            if (r > 0)
            {
                ans += r;
            }
        }
        else
        {
            if (l <= 0)
            {
                r++;
            }
            else
            {
                l--;
            }
        }
    }
    cout << ans << endl;
}
int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
